<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slide_Option_Schedule_Data
 * @category   Model
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Slide_Option_Schedule_Data extends Ot_Db_Table
{
    /**
     * Name of the table in the database
     *
     * @var string
     */
    protected $_name = 'tbl_slide_option_schedule';
  
    /**
     * Primary key of the table
     *
     * @var string
     */
    protected $_primary = 'slideId';

    /**
     * Generates the form for adding and editing a slide's schedule data
     * 
     * @param array $values The values the form should be populated with
     * @return Zend_Form
     */
    public function form($values = array())
    {
        $form = new Zend_Form();
        $form->setAttrib('class', 'slideOptionForm')
             ->setDecorators(array(
                     'FormElements',
                     array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form')),
                     'Form'
             ));
             
        $fromDate = $form->createElement('text', 'fromDate', array('label' => 'From:'));
        $fromDate->setRequired(false)
                 ->addFilter('StringTrim')
                 ->addFilter('StripTags')
                 ->setAttrib('maxlength', '128')
                 ->setAttrib('style', 'width: 80px')
                 ->setValue(isset($values['fromDate']) ? strftime('%A, %B %e, %Y', strtotime($values['fromDate'])) : '');
        
        $toDate = $form->createElement('text', 'toDate', array('label' => 'To:'));
        $toDate->setRequired(false)
               ->addFilter('StringTrim')
               ->addFilter('StripTags')
               ->setAttrib('maxlength', '128')
               ->setAttrib('style', 'width: 80px')
               ->setValue(isset($values['toDate']) ? strftime('%A, %B %e, %Y', strtotime($values['toDate'])) : '');
        
        // add the from time selector
        $fromTimeSub = new Zend_Form_SubForm();
        $fromTimeSub->setDecorators(array(
                     'FormElements',
                     array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form'))
                ));
        $fromTimeSub->setAttrib('class', 'sub');
        $fromTimeHour = $fromTimeSub->createElement('select', 'hour');
        $fromTimeHour->setAttrib('id', 'fromHour');
        for ($i = 1; $i <= 12; $i++) {
            $fromTimeHour->addMultiOption($i, $i);
        }
        $fromTimeHour->setDecorators(array(
                array('ViewHelper', array('helper' => 'formSelect')),
                array('Label')
            ));
        $fromTimeHour->setValue((isset($values['fromTime'])) ? date('g', strtotime($values['fromTime'])) : date('g'));
        
        $fromTimeMinute = $fromTimeSub->createElement('select', 'minute');
        $fromTimeMinute->setAttrib('id', 'fromMinute');
        for ($i = 0; $i < 60; $i += 5) {
            $fromTimeMinute->addMultiOption(str_pad($i, 2, '0', STR_PAD_LEFT), str_pad($i, 2, '0', STR_PAD_LEFT));
        }
        $fromTimeMinute->setDecorators(array(
                array('ViewHelper', array('helper' => 'formSelect'))
            ));
        $fromTimeMinute->setValue((isset($values['fromTime'])) ? date('i', strtotime($values['fromTime'])) : date('i'));
            
        $fromTimeMeridian = $fromTimeSub->createElement('select', 'meridian');
        $fromTimeMeridian->setAttrib('id', 'fromMeridian')
               ->addMultiOption('am', 'AM')
               ->addMultiOption('pm', 'PM')
               ->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSelect'))
               ));
        $fromTimeMeridian->setValue((isset($values['fromTime'])) ? date('a', strtotime($values['fromTime'])) : date('a'));       
               
        $fromTimeSub->addElements(array($fromTimeHour, $fromTimeMinute, $fromTimeMeridian));

        // add the to time selector
        $toTimeSub = new Zend_Form_SubForm();
        $toTimeSub->setDecorators(array(
                     'FormElements',
                     array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form'))
                     ));
        $toTimeSub->setAttrib('class', 'sub');
        $toTimeHour = $toTimeSub->createElement('select', 'hour');
        $toTimeHour->setAttrib('id', 'toHour');
        for ($i = 1; $i <= 12; $i++) {
            $toTimeHour->addMultiOption($i, $i);
        }
        $toTimeHour->setDecorators(array(
                array('ViewHelper', array('helper' => 'formSelect')),
                array('Label')
            ));
        $toTimeHour->setValue((isset($values['toTime'])) ? date('g', strtotime($values['toTime'])) : date('g'));
        
        $toTimeMinute = $toTimeSub->createElement('select', 'minute');
        $toTimeMinute->setAttrib('id', 'toMinute');
        for ($i = 0; $i < 60; $i += 5) {
            $toTimeMinute->addMultiOption(str_pad($i, 2, '0', STR_PAD_LEFT), str_pad($i, 2, '0', STR_PAD_LEFT));
        }
        $toTimeMinute->setDecorators(array(
                array('ViewHelper', array('helper' => 'formSelect'))
            ));
        $toTimeMinute->setValue((isset($values['toTime'])) ? date('i', strtotime($values['toTime'])) : date('i'));
            
        $toTimeMeridian = $toTimeSub->createElement('select', 'meridian');
        $toTimeMeridian->setAttrib('id', 'toMeridian')
               ->addMultiOption('am', 'AM')
               ->addMultiOption('pm', 'PM')
               ->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSelect'))
               ));
        $toTimeMeridian->setValue((isset($values['toTime'])) ? date('a', strtotime($values['toTime'])) : date('a'));       
               
        $toTimeSub->addElements(array($toTimeHour, $toTimeMinute, $toTimeMeridian));
        
        /**
        $submit = $form->createElement('submit', 'submitButton', array('label' => 'Submit'));
        $submit->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSubmit'))
                 ));

        $cancel = $form->createElement('button', 'cancel', array('label' => 'Cancel'));
        $cancel->setAttrib('id', 'cancel');
        $cancel->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formButton'))
                ));
        **/
        
        $form->addElements(array($fromDate))
             ->addSubForms(array('fromTime' => $fromTimeSub))
             ->addElements(array($toDate))
             ->addSubForms(array('toTime' => $toTimeSub));

        $form->setElementDecorators(array(
                  'ViewHelper',
                  'Errors',
                  array('HtmlTag', array('tag' => 'div', 'class' => 'elm')),
                  array('Label', array('tag' => 'span')),
              ));
             //->addElements(array($submit, $cancel));

        $slideId = $form->createElement('hidden', 'slideId');
        $slideId->setValue($values['slideId']);
        $slideId->setDecorators(array(
            array('ViewHelper', array('helper' => 'formHidden'))
        ));

        $form->addElement($slideId);
        
        return $form;
    }    
}